package com.sk.cardeal.dialog;

import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.cardeal.R;
import com.sk.cardeal.act.home.activity.ChoosePartsActivity;
import com.sk.cardeal.adapter.BuyDialogAdapter;
import com.sk.cardeal.callback.MyCallBack;
import com.sk.cardeal.entity.Parts;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by sk on 2017/11/19.
 */

public class BuyNextDialogFragment extends DialogFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {

    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.apply_form)
    TextView applyForm;
    private MyCallBack myCallBack;

    private List<Parts> selectParts;
    private BuyDialogAdapter buyDialogAdapter;

    public MyCallBack getMyCallBack() {
        return myCallBack;
    }

    public void setMyCallBack(MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
    }

    Unbinder unbinder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int style = android.support.v4.app.DialogFragment.STYLE_NO_TITLE, theme = 0;
        setStyle(style, theme);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bug_next_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getActivity() instanceof ChoosePartsActivity) {
            selectParts = ((ChoosePartsActivity) getActivity()).getSelectParts();
            dataRyc.setLayoutManager(new LinearLayoutManager(getActivity()));
            buyDialogAdapter = new BuyDialogAdapter(R.layout.item_buy_dialog, selectParts);
            dataRyc.setAdapter(buyDialogAdapter);
            buyDialogAdapter.setOnItemChildClickListener(this);
            buyDialogAdapter.setOnItemClickListener(this);
            tvTip.setText("已选择" + caculateTotal() + "件商品");
        }
    }


    public void onStart() {
        super.onStart();
        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialogAnim;
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes(layoutParams);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_tip, R.id.apply_form})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_tip:
                dismiss();
                break;
            case R.id.apply_form:
                dismiss();
                myCallBack.sure();
                break;
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.add:
                int count = selectParts.get(position).getSelectCount();
                count++;
                selectParts.get(position).setSelectCount(count);
                adapter.notifyItemChanged(position);
                tvTip.setText("已选择" + caculateTotal() + "件商品");
                myCallBack.doChange();
                break;
            case R.id.reduce:
                int count1 = selectParts.get(position).getSelectCount();
                if (count1 > 0) {
                    count1--;
                    selectParts.get(position).setSelectCount(count1);
                    if (count1 == 0) {
                        selectParts.remove(position);
                        adapter.notifyItemRemoved(position);
                    } else {
                        adapter.notifyItemChanged(position);
                    }
                    tvTip.setText("已选择" + caculateTotal() + "件商品");
                }
                myCallBack.doChange();
                break;
        }
    }

    private int caculateTotal() {
        int total = 0;
        for (Parts parts : selectParts) {
            total += parts.getSelectCount();
        }
        return total;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }
}
